package learn;

import org.junit.Test;

import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @author wujf
 * @date 2024-02-29
 * @description 描述
 */
public class CoroutineLearn {

    @Test
    public void ts0() {
        UserInfo userInfo = new UserInfo("username", "password");
        System.out.println(userInfo);
    }

    @Test
    public void ts1() {
        Thread.startVirtualThread(() -> {
            System.out.println("我是虚拟线程(协程)");
        });
        System.out.println("我是主线程");
    }

    // 虚拟线程不需要配置核心线程 最大线程 队列等参数
    // 一般通过信号量来控制最大线程数
    @Test
    public void ts2() {
        var semaphore = new Semaphore(50);
        try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
            executor.submit(() -> System.out.println("ts2虚拟线程"));

            executor.submit(() -> {
                        try {
                            semaphore.acquire();
                            System.out.println("拿到信号量");
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        } finally {
                            semaphore.release();
                        }
                    }
            );
        }

    }


}
